package com.journey.org.ui.home.news

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayout
import com.journey.org.BR
import com.journey.org.R
import com.journey.org.databinding.FragmentNewsBinding
import com.journey.org.ui.home.news.list.NewsListFragment
import com.mvvmhabit.org.base.BaseFragment
import com.mvvmhabit.org.base.BaseFragmentPagerAdapter
import com.mvvmhabit.org.custom.SpinnerItemData

/**
 * @Date: 2020/11/23 9:47
 * @Author:小妖
 * @Description: 新闻
 */
class NewsFragment : BaseFragment<FragmentNewsBinding, NewsViewModel>() {

    private val mFragment = ArrayList<Fragment>()

    private val mTitle = ArrayList<String>()

    private val mItemData = ArrayList<SpinnerItemData>()

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): Class<NewsViewModel> = NewsViewModel::class.java

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.fragment_news

    override fun initData() {
        super.initData()
        mItemData.add(SpinnerItemData("headline", "头条"))
        mItemData.add(SpinnerItemData("T1348649079062", "体育"))
        mItemData.add(SpinnerItemData("T1348649580692", "科技"))
        mItemData.add(SpinnerItemData("T1348648650048", "电影"))
        mItemData.add(SpinnerItemData("T1348654060988", "汽车"))
        mItemData.add(SpinnerItemData("T1350383429665", "笑话"))
        mItemData.add(SpinnerItemData("T1348654151579", "游戏"))
        mItemData.add(SpinnerItemData("T1348654204705", "旅游"))
        mItemData.add(SpinnerItemData("T1348649654285", "手机"))
        mItemData.add(SpinnerItemData("T1348648141035", "军事"))

        mItemData.forEach { item ->
            val fragment = NewsListFragment()
            val bundle = Bundle()
            bundle.putString("type", item.getKey())
            fragment.arguments = bundle
            mFragment.add(fragment)
            mTitle.add(item.getValue()!!)
        }
        initAdapter()
    }


    private fun initAdapter() {
        mBinding.layoutNewsViewpager.offscreenPageLimit = 1
        val adapter = BaseFragmentPagerAdapter(activity!!.supportFragmentManager, mFragment, mTitle)
        mBinding.layoutNewsViewpager.adapter = adapter
        mBinding.layoutNewsTab.setupWithViewPager(mBinding.layoutNewsViewpager)
        mBinding.layoutNewsViewpager.addOnPageChangeListener(
            TabLayout.TabLayoutOnPageChangeListener(
                mBinding.layoutNewsTab
            )
        )
    }
}